import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QVBoxLayout, QGridLayout, QWidget, QPushButton


class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('计算器')
        layout = QVBoxLayout()

        edit = QLineEdit()
        # 设置占位文本
        edit.setPlaceholderText('请输入内容')
        layout.addWidget(edit)

        # 网格布局器
        grid = QGridLayout()

        # 布局内容
        data = {
            0: ['7', '8', '9', '+', '('],
            1: ['4', '5', '6', '-', ')'],
            2: ['1', '2', '3', '*', '<-'],
            3: ['0', '.', '=', '/', 'C']
        }

        for row, lis in data.items():
            for col, name in enumerate(lis):
                btn = QPushButton(name)
                grid.addWidget(btn, row, col)
        layout.addLayout(grid)

        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    my_widget = MyWidget()
    my_widget.show()
    app.exec_()
